package com.rmt.www.daoImpl;

import java.util.List;

import javax.sql.DataSource;

import oracle.net.aso.n;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.rmt.www.constant.ArtifactStatus;
import com.rmt.www.dao.ImplementationArtifactDao;
import com.rmt.www.vo.Artifact;
import com.rmt.www.vo.ImplementationArtifact;

public class ImplementationArtifactDaoImpl implements ImplementationArtifactDao {

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public int insertImplementationArtifact(ImplementationArtifact artifact) {
		jdbcTemplate.update("insert into implartifact(pid,title,function,input,output,processingsteps,algorithm,errorhandling,code,priority,effort,cost,risk,comment,createrid,createdtime,updaterid,updatedtime,updatedtimes,version,status,language)"+
			"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
	new Object[]{
		artifact.getPid(),
		artifact.getTitle(),
		artifact.getFunction(),
		artifact.getInput(),
		artifact.getOutput(),
		artifact.getProcessingsteps(),
		artifact.getAlgorithm(),
		artifact.getErrorhandling(),
		artifact.getCode(),
		artifact.getPriority(),
		artifact.getEffort(),
		artifact.getCost(),
		artifact.getRisk(),
		artifact.getComment(),
		artifact.getCreaterid(),
		artifact.getCreatedtime(),
		artifact.getUpdaterid(),
		artifact.getUpdatedtime(),
		artifact.getUpdatedtimes(),
		artifact.getVersion(),
		artifact.getStatus(),
		artifact.getLanguage()
	},new int[] {
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.INTEGER,
		java.sql.Types.TIMESTAMP,
		java.sql.Types.INTEGER,
		java.sql.Types.TIMESTAMP,
		java.sql.Types.INTEGER,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR,
		java.sql.Types.VARCHAR
	});
int lastInsertId = jdbcTemplate.queryForInt("select last_insert_id()");
return lastInsertId;
	}
	
	public void updateImplementationArtifact(ImplementationArtifact newArtifact) {
		jdbcTemplate.update("update implartifact " +
				"set pid=?,title=?,function=?,input=?,output=?,processingsteps=?,algorithm=?,code=?,priority=?,effort=?,cost=?," +
				"risk=?,comment=?,createrid=?,createdtime=?,updaterid=?,updatedtime=?,updatedtimes=?,version=?,status=?,language=?" +
				"where id=?", 
				new Object[]{
					newArtifact.getPid(),
					newArtifact.getTitle(),
					newArtifact.getFunction(),
					newArtifact.getInput(),
					newArtifact.getOutput(),
					newArtifact.getProcessingsteps(),
					newArtifact.getAlgorithm(),
					newArtifact.getErrorhandling(),
					newArtifact.getCode(),
					newArtifact.getPriority(),
					newArtifact.getEffort(),
					newArtifact.getCost(),
					newArtifact.getRisk(),
					newArtifact.getComment(),
					newArtifact.getCreaterid(),
					newArtifact.getCreatedtime(),
					newArtifact.getUpdaterid(),
					newArtifact.getUpdatedtime(),
					newArtifact.getUpdatedtimes(),
					newArtifact.getVersion(),
					newArtifact.getStatus(),
					newArtifact.getLanguage(),
					newArtifact.getId()
			}, new int[] {
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.INTEGER,
				java.sql.Types.TIMESTAMP,
				java.sql.Types.INTEGER,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.VARCHAR,
				java.sql.Types.INTEGER
			});

	}
	
	public void deleteImplementationArtifactById(int id) {
		jdbcTemplate.update("delete from implartifact where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER});

	} 

	@SuppressWarnings("unchecked")
	public List<ImplementationArtifact> getImplementationArtifactsByPId(int pid) {
		return (List<ImplementationArtifact>)(this.jdbcTemplate.query("select * from implartifact where status<>? and pid=?",
				new Object[]{ArtifactStatus.DELETED, pid},
				new int[]{ java.sql.Types.VARCHAR, java.sql.Types.INTEGER}, new ImplementationArtifactRowMapper()));
	}
	

	public ImplementationArtifact getImplementationArtifactById(int id) {
		try {	
			return  (ImplementationArtifact) this.jdbcTemplate.queryForObject("select * from implartifact where id = ?",
					new Object[]{id}, new ImplementationArtifactRowMapper());
		  }catch (final EmptyResultDataAccessException e) {
			  return null;
		  }
	}


}
